package com.jy.adscm.operation.cgxsb.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.operation.cgxsb.bean.BaseTaskdetailinfo;
import com.jy.adscm.operation.cgxsb.bean.CgxsTaskinfo;
import com.jy.adscm.operation.cgxsb.service.BaseTaskdetailinfoService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyy
 * @since 2019-01-13
 */
@RestController
@RequestMapping("adscm/operation/baseTaskdetailinfo")
public class BaseTaskdetailinfoController {

    @Autowired
    BaseTaskdetailinfoService baseTaskdetailinfoService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @RequestMapping("/insert")
    public SimpleCoreResponse insert(String data,HttpServletRequest request){
        BaseTaskdetailinfo baseTaskdetailinfo= null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            baseTaskdetailinfo = mapper.readValue(data, BaseTaskdetailinfo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        baseTaskdetailinfo.setTxr(String.valueOf(token.getUser().getId()));
        baseTaskdetailinfo.setTdid(UUID.randomUUID().toString());
        baseTaskdetailinfo.setTdsj(new Date());
        return new SimpleCoreResponse("200","新增成功",baseTaskdetailinfoService.insert(baseTaskdetailinfo));
    }

    @RequestMapping("/update")
    public SimpleCoreResponse update(String data){
        BaseTaskdetailinfo baseTaskdetailinfo= null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            baseTaskdetailinfo = mapper.readValue(data, BaseTaskdetailinfo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new SimpleCoreResponse("200","修改成功",baseTaskdetailinfoService.updateById(baseTaskdetailinfo));
    }
    @RequestMapping("/delete")
    public SimpleCoreResponse delete(String data){
        BaseTaskdetailinfo baseTaskdetailinfo= null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            baseTaskdetailinfo = mapper.readValue(data, BaseTaskdetailinfo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new SimpleCoreResponse("200","删除成功",baseTaskdetailinfoService.deleteById(baseTaskdetailinfo));
    }

    @RequestMapping("/queryByTid")
    public SimpleCoreResponse queryByTid(String tid){
        return new SimpleCoreResponse("200","查询成功",baseTaskdetailinfoService.queryByTid(tid));
    }
}

